﻿using System;
using System.Diagnostics.Contracts;
using Wombat.Core.Ports;

namespace Wombat.Core.Factories
{
    public class PortFactory
    {
        public static IPort Create(string portName, PortBehavior portBehavior, ITagValueObservable observableHost)
        {
            return Create<ITagValue>(portName, portBehavior, observableHost: observableHost);
        }

        public static IPort Create<T>(string portName, PortBehavior portBehavior, Action<T> observerCall = null, 
            Action<Exception> onError = null, Action onCompleted = null, ITagValueObservable observableHost = null) where T:ITagValue
        {
            switch (portBehavior)
            {
                    case PortBehavior.Input:
                        Contract.Assert(observerCall != null);
                        return new GenericObserverPort<T>(portName, Guid.NewGuid(), portBehavior, observerCall, onError, onCompleted);
                    case PortBehavior.Output:
                        Contract.Assert(observableHost != null);
                        return new GenericObservablePort(portName, portBehavior, observableHost);
                    case PortBehavior.Duplex:
                        throw new NotImplementedException();
                    case PortBehavior.Nill:
                        throw new NotImplementedException();
            }
            return null;
        }
    }
}
